using System;
using System.Collections.Generic;

namespace PSSymbolStore
{
    public class AddTransaction : Transaction
    {
        public AddTransaction()
        {
            TransactionType = TransactionType.Add;
        }

        public AddTransaction(TransactionType transactionType)
            : base(transactionType)
        {
            TransactionType = TransactionType.Add;
        }

        public AddTransaction(string transactionId, TransactionType transactionType)
            : base(transactionId, transactionType)
        {
            TransactionType = TransactionType.Add;
        }

        private DateTime _transactionDateTime = DateTime.Now;

        public DateTime TransactionDateTime
        {
            get { return _transactionDateTime; }
            set
            {
                if (_transactionDateTime != value)
                {
                    _transactionDateTime = value;
                    OnTransactionDateTimeChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler TransactionDateTimeChanged;

        protected void OnTransactionDateTimeChanged(EventArgs e)
        {
            if (TransactionDateTimeChanged != null)
            {
                TransactionDateTimeChanged(this, e);
            }
        }

        private TransactionUploadType _fileOrPointer;

        public TransactionUploadType FileOrPointer
        {
            get { return _fileOrPointer; }
            set
            {
                if (_fileOrPointer != value)
                {
                    _fileOrPointer = value;
                    OnFileOrPointerChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler FileOrPointerChanged;

        protected void OnFileOrPointerChanged(EventArgs e)
        {
            if (FileOrPointerChanged != null)
            {
                FileOrPointerChanged(this, e);
            }
        }

        private string _product;

        public string Product
        {
            get { return _product; }
            set
            {
                if (_product != value)
                {
                    _product = value;
                    OnProductChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler ProductChanged;

        protected void OnProductChanged(EventArgs e)
        {
            if (ProductChanged != null)
            {
                ProductChanged(this, e);
            }
        }

        private string _version;

        public string Version
        {
            get { return _version; }
            set
            {
                if (_version != value)
                {
                    _version = value;
                    OnVersionChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler VersionChanged;

        protected void OnVersionChanged(EventArgs e)
        {
            if (VersionChanged != null)
            {
                VersionChanged(this, e);
            }
        }

        private string _comment;

        public string Comment
        {
            get { return _comment; }
            set
            {
                if (_comment != value)
                {
                    _comment = value;
                    OnCommentChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler CommentChanged;

        protected void OnCommentChanged(EventArgs e)
        {
            if (CommentChanged != null)
            {
                CommentChanged(this, e);
            }
        }

        private bool _locked;

        public bool Locked
        {
            get { return _locked; }
            set
            {
                if (_locked != value)
                {
                    _locked = value;
                    OnLockedChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler LockedChanged;

        protected void OnLockedChanged(EventArgs e)
        {
            if (LockedChanged != null)
            {
                LockedChanged(this, e);
            }
        }

        private List<TransactionFile> _files = new List<TransactionFile>();

        public List<TransactionFile> Files
        {
            get { return _files; }
            set
            {
                if (_files != value)
                {
                    _files = value;
                    OnFilesChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler FilesChanged;

        protected void OnFilesChanged(EventArgs e)
        {
            if (FilesChanged != null)
            {
                FilesChanged(this, e);
            }
        }

        internal void Fill()
        {
            //try
            //{
            //    // Open the transaction file and retrieve the list of files
            //    // associated with this transaction
            //    string path = Store.GetTransactionFile(TransactionId);
            //    if (File.Exists(path))
            //    {
            //        List<TransactionFile> files = new List<TransactionFile>();
            //        using (StreamReader reader = File.OpenText(path))
            //        {
            //            string line = reader.ReadLine();
            //            while (line != null)
            //            {
            //                try
            //                {
            //                    List<string> lineParts = Helpers.ParseCSVLine(line);
            //                    if (lineParts.Count == 2)
            //                    {
            //                        files.Add(new TransactionFile()
            //                        {
            //                            SymbolStoreLocation = lineParts[0].Trim('"'),
            //                            OriginalLocation = lineParts[1].Trim('"')
            //                        });
            //                    }
            //                }
            //                catch
            //                {
            //                    // Ignore, continue
            //                }

            //                line = reader.ReadLine();
            //            }
            //        }
            //        this.Files = files;
            //    }
            //}
            //catch (UnauthorizedAccessException uae)
            //{
            //    Store.Cmdlet.WriteDebug("Unauthorized access: " + uae.Message);
            //}
        }

        private static string GetTransactionUploadTypeString(TransactionUploadType type)
        {
            switch (type)
            {
                case TransactionUploadType.File:
                    return "file";
                case TransactionUploadType.Pointer:
                    return "ptr";
                default:
                    return String.Empty;
            }
        }

        public override string ToString()
        {
            // An example:
            //0000000023,add,file,11/23/2007,02:07:38,"MyProduct","1.4.2","Hey there",
            return String.Format("{0},add,{1},{2},{3},\"{4}\",\"{5}\",\"{6}\",",
                TransactionId,
                GetTransactionUploadTypeString(FileOrPointer),
                TransactionDateTime.ToString("d", System.Globalization.CultureInfo.InvariantCulture),
                TransactionDateTime.ToString("T", System.Globalization.CultureInfo.InvariantCulture),
                (Product != null) ? Product.Trim('"') : "",
                (Version != null) ? Version.Trim('"') : "",
                (Comment != null) ? Comment.Trim('"') : "");
        }
    }
}
